'use strict';

/* Controllers */

angular.module('app')
  .controller('AppCtrl', ['$scope', '$localStorage', '$window', '$rootScope', '$modal',
    function(              $scope, $localStorage,   $window, $rootScope, $modal ) {
      // add 'ie' classes to html
      var isIE = !!navigator.userAgent.match(/MSIE/i);
      isIE && angular.element($window.document.body).addClass('ie');
      isSmartDevice( $window ) && angular.element($window.document.body).addClass('smart');

      $scope.base_url = window.STATIC_BASE_URL;
      $scope.USER = window.__USER__;
      $scope.app = {
        name: '象呈后台管理',
        version: '1.0.0',
        color: {
          primary: '#7266ba',
          info:    '#23b7e5',
          success: '#27c24c',
          warning: '#fad733',
          danger:  '#f05050',
          light:   '#e8eff0',
          dark:    '#3a3f51',
          black:   '#1c2b36'
        },
        settings: {
          themeID: 1,
          navbarHeaderColor: 'bg-black',
          navbarCollapseColor: 'bg-white-only',
          asideColor: 'bg-black',
          headerFixed: true,
          asideFixed: false,
          asideFolded: false,
          asideDock: false,
          container: false
        }
      }

      // save settings to local storage
      if ( angular.isDefined($localStorage.settings) ) {
        $scope.app.settings = $localStorage.settings;
      } else {
        $localStorage.settings = $scope.app.settings;
      }
      $scope.$watch('app.settings', function(){
        if( $scope.app.settings.asideDock  &&  $scope.app.settings.asideFixed ){
          // aside dock and fixed must set the header fixed.
          $scope.app.settings.headerFixed = true;
        }
        // for box layout, add background image
        $scope.app.settings.container ? angular.element('html').addClass('bg') : angular.element('html').removeClass('bg');
        // save to local storage
        $localStorage.settings = $scope.app.settings;
      }, true);

      function isSmartDevice( $window )
      {
          // Adapted from http://www.detectmobilebrowsers.com
          var ua = $window['navigator']['userAgent'] || $window['navigator']['vendor'] || $window['opera'];
          // Checks for iOs, Android, Blackberry, Opera Mini, and Windows mobile devices
          return (/iPhone|iPod|iPad|Silk|Android|BlackBerry|Opera Mini|IEMobile/).test(ua);
      }

      $rootScope.dialog = function(templateUrl, controllerName, resolve, settings) {
          var option = {};
          if (controllerName) {
              option = {
                  templateUrl: templateUrl,
                  controller: controllerName,
                  resolve: resolve || {},
                  backdrop: 'static'
              };

              if (settings && settings.size) {
                  option.size = settings.size;
              }
              if (settings && settings.windowClass) {
                  option.windowClass = settings.windowClass;
              }
          } else {
              option = {
                  templateUrl: templateUrl
              };
          }
          var modalInstance = $modal.open(option);
          return modalInstance;
      }

      $rootScope.confirm = function(context, title) {
          return $rootScope.dialog("src/app/tpl/confirm-dialog.html", "ConfirmDialogCtrl", {
              item: function() {
                  return {
                      title: title,
                      context: context
                  }
              }
          }, { size: 'sm' }).result;
      }


      $rootScope.getSgObj = function (key) {
            var obj = $window.sessionStorage.getItem(key);
            return JSON.parse(obj);
      }

      $rootScope.setSgObj = function (key, value) {
            return $window.sessionStorage.setItem(key, JSON.stringify(value));
      }

  }]);

  angular.module('app')
  .controller("ConfirmDialogCtrl", ["$rootScope", "$scope", "$state", '$modalInstance', 'item', function($rootScope, $scope, $state, $modalInstance, item) {
    $scope.model = {
        title: '',
        context: ''
    };

    $scope.ok = function() {
        $modalInstance.close('success');
    };

    $scope.cancel = function() {
        $modalInstance.dismiss('cancel');
    };

    var init = function() {
        if (item) {
            $scope.model.title = item.title;
            $scope.model.context = item.context;
        }
    };
    init();
  }]);

  angular.module('app')
  .controller('NavCtrl', ['$scope', function ($scope) {
    $scope.navCollections = [
      {
       'name': '产品管理',
       'href': 'app.product',
       'icon': 'magnet',
       'auth': '',
       'class': 'glyphicon',
       'sublist':[{
         'name':'产品列表',
         'auth':'',
         'href':'app.product.list'
        },{
         'name':'产品审核',
         'auth':'',
         'href':'app.product.audit'
        }]
      },{
       'name': '需求管理',
       'href': 'app.demand',
       'icon': 'home',
       'auth': '',
       'class': 'glyphicon',
       'sublist':[{
         'name':'需求列表',
         'auth':'',
         'href':'app.demand.list'
        },{
         'name':'需求审核',
         'auth':'',
         'href':'app.demand.audit'
        }]
      },{
       'name': '用户管理',
       'href': 'app.user',
       'icon': 'user',
       'auth': '',
       'class': 'glyphicon',
       'sublist':[{
         'name':'用户列表',
         'auth':'',
         'href':'app.user.list'
        }]
      },{
       'name': '营销管理',
       'href': 'app.banner',
       'icon': 'pencil',
       'auth': '',
       'class': 'glyphicon',
       'sublist':[{
         'name':'广告位管理',
         'auth':'',
         'href':'app.banner.list'
        }]
      },{
       'name': '标签管理',
       'href': 'app.tag',
       'icon': 'tag',
       'auth': '',
       'class': 'glyphicon',
       'sublist':[{
         'name':'标签管理',
         'auth':'',
         'href':'app.tag.list'
        }]
      },{
       'name': '订金管理',
       'href': 'app.money',
       'icon': 'yen',
       'auth': '',
       'class': 'glyphicon',
       'sublist':[{
         'name':'订金列表',
         'auth':'',
         'href':'app.money.list'
        }]
      }
    ];
  }]);